<?php

namespace App\Http\Controllers\Admin\ContentManager;

use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Response\Response;

class FaceController extends BaseController {

    /**
     * @api {post} /content-manager/face 表情创建
     * @apiVersion 0.1.0
     * @apiName 内容管理-表情创建
     * @apiGroup 内容管理
     *
     *
     * @apiParam {number} sort                  排序 必传
     * @apiParam {string} name                  名称 必传
     * @apiParam {string} picture               图片 必传
     * @apiParam {string} special               表情 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function create(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(), [
                'sort' => 'required|numeric|min:1|max:2100000000',
                'name' => 'required|string|max:255',
                'picture' => 'required|active_url',
                'special' => 'required|active_url',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $params = $request->input();

        $res = app('repo_content');
        $rows = $res->addFace($params);
        if (!$rows) {
            throw new BasicException(10001, "插入失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {delete} /content-manager/face 表情删除
     * @apiVersion 0.1.0
     * @apiName 内容管理-表情删除
     * @apiGroup 内容管理
     *
     * @apiParam {string} ids                       字符串分割的id 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function del(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'ids' => 'required|string',
            ]);

        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $idsStr = $request->input("ids");
        $idsArr = explode("," , $idsStr);

        $rows = app('repo_content')->delFace($idsArr);
        if (!$rows) {
            throw new BasicException(10001, "删除失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {put} /content-manager/face 表情修改
     * @apiVersion 0.1.0
     * @apiName 内容管理-表情修改
     * @apiGroup 内容管理
     *
     *
     * @apiParam {number} id
     * @apiParam {number} sort                  排序 必传
     * @apiParam {string} name                  名称 必传
     * @apiParam {string} picture               图片 必传
     * @apiParam {string} special               表情 必传
     *
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 10001 参数校验失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function update(Request $request) {
        //参数校验
        $v = app('validator')->make($request->all(), [
                'id' => 'required|integer',
                'sort' => 'required|numeric|min:1|max:2100000000',
                'name' => 'required|string|max:255',
                'picture' => 'required|string|active_url',
                'special' => 'required|string|active_url',
            ]);
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));

        $res = app('repo_content');

        $params = $request->input();
        $id = $params['id'];

        $rows = $res->saveFace(["id" => $id], $params);
        if (!$rows) {
            throw new BasicException(10001, "更新失败！");
        }

        return Response::sendData(true);
    }

    /**
     * @api {get} /content-manager/face 表情列表
     * @apiVersion 0.1.0
     * @apiName 内容管理-表情列表
     * @apiGroup 内容管理
     *
     * @apiParam {number} keyword 表情名称 可选
     * @apiParam {number} page 当前页数 可选
     * @apiParam {number} perPage 分页容量 可选
     *
     *
     * @apiSuccessExample 成功响应
     *
     *
    {
    "code": 200,
    "data": {
        "list": [
            {
                "id(序号)": 66,
                "name(名称)": "关灯再见",
                "picture(图片)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f8964d33aac4.png",
                "special(表情)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f8965006836e.webp",
                "sort(排序)": 1
            }
        ],
        "meta": {
                "page": 1,
                "limit": 10,
                "next": null,
                "total": 1
            }
        }
     }
     *
     *
     */
    public function list(Request $request) {
        //参数校验
        try {
            $this->validate($request, [
                'keyword' => 'filled|string',
                'perPage' => 'filled|numeric',
                'page' => 'filled|numeric',
            ]);
        } catch (\Exception $e) {
            throw new BasicException(10001, json_encode($e->errors()));
        }

        $list = app('repo_content')->getFaceQueryList($request->input());

        return Response::sendList($list);
    }


}
